package edu.jxut.reggie.common;


import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


@Component
public class PicUploadServer {

    @Autowired
    protected FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;


    public String uploadJPG(MultipartFile multipartFile) {
        try {
            InputStream inputStream = multipartFile.getInputStream();
            StorePath storePath = this.storageClient.uploadFile(inputStream,multipartFile.getSize(),"jpg",null);
//            System.out.println(storePath); //StorePath [group=group1, path=M00/00/00/wKgfUV2GJSuAOUd_AAHnjh7KpOc1.1.jpg]
            return fdfsWebServer.getWebServerUrl() +"/"+storePath.getFullPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String uploadJPG(FileInputStream inputStream) {
        try {
            StorePath storePath = this.storageClient.uploadFile(inputStream,inputStream.getChannel().size(), "jpg",null);
//            System.out.println(storePath); //StorePath [group=group1, path=M00/00/00/wKgfUV2GJSuAOUd_AAHnjh7KpOc1.1.jpg]
            return fdfsWebServer.getWebServerUrl() +"/"+storePath.getFullPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String uploadMP4(MultipartFile multipartFile) {
        try {
            InputStream inputStream = multipartFile.getInputStream();
            StorePath storePath = this.storageClient.uploadFile(inputStream,multipartFile.getSize(),"mp4",null);
//            System.out.println(storePath); //StorePath [group=group1, path=M00/00/00/wKgfUV2GJSuAOUd_AAHnjh7KpOc1.1.jpg]
//            System.out.println(fdfsWebServer.getWebServerUrl()+"/" + storePath.getFullPath());//group1/M00/00/00/wKgfUV2GJSuAOUd_AAHnjh7KpOc1.1.jpg
            return fdfsWebServer.getWebServerUrl() +"/"+ storePath.getFullPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}